<template>
	<div class="system-teach-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="teachDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="教师id" prop="teacherId">
							<el-input v-model="state.ruleForm.teacherId" placeholder="" disabled></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="教师姓名" prop="teacherId">
              <el-input v-model="state.ruleForm.teacherName" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="科目" prop="idType">
              <el-select v-model="state.ruleForm.teachSubject" placeholder="请选择科目" clearable class="w100">
                <el-option label="班主任" value="班主任"></el-option>
                <el-option label="语文" value="语文"></el-option>
                <el-option label="数学" value="数学"></el-option>
                <el-option label="英语" value="英语"></el-option>
                <el-option label="物理" value="物理"></el-option>
                <el-option label="化学" value="化学"></el-option>
                <el-option label="生物" value="生物"></el-option>
                <el-option label="历史" value="历史"></el-option>
                <el-option label="地理" value="地理"></el-option>
                <el-option label="科学" value="科学"></el-option>
                <el-option label="思想品德" value="思想品德"></el-option>
                <el-option label="信息技术" value="信息技术"></el-option>
                <el-option label="体育" value="体育"></el-option>
                <el-option label="道德与法治" value="道德与法治"></el-option>
                <el-option label="思想政治" value="思想政治"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="班级" prop="orgId">
              <el-tree-select
                  v-model="state.ruleForm.orgId"
                  :data="state.sysOrgData"
                  :render-after-expand="false"
                  style="width: 500px"
                  filterable
                  show-all-levels
              />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
              <el-select v-model="state.ruleForm.isDelete" placeholder="请选择"  class="w100" :disabled="state.ruleForm.isDelete==0">
                <el-option label="正常"  :value="0"></el-option>
                <el-option label="禁用"  :value="1"></el-option>
              </el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(teachDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="TeachDialog">
import { reactive, ref} from 'vue';
import {systemApi} from "/@/api/system";
import {userApi} from "/@/api/user";
import {ElMessage, FormInstance, FormRules} from 'element-plus'
const {orgSchoolTree} = systemApi();
const {saveOrUpdateTeach} = userApi();
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const rules = reactive<FormRules<RowTeachType>>({

  teachSubject: [
    {
      required: true,
      message: '请选择科目',
      trigger: 'change',
    },
  ],
  orgId: [
    {
      required: true,
      message: '请选择班级',
      trigger: 'change',
    },
  ],

})

// 定义变量内容
const teachDialogFormRef = ref();
const state = reactive({
	ruleForm: {
    id:'',
    isDelete:0,
    isHeadTeacher:0,
    orgId:'',
    orgName:'',
    teachSubject:'',
    teacherId:'',
    teacherName:'',
	},
  teacherRow:{} as RowStudentUserType,
  sysOrgData: [] as OrgTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, teacherRow: RowStudentUserType,row: RowTeachType) => {
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改任教信息';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增任教信息';
		state.dialog.submitTxt = '新 增';
    clearForm();
    state.ruleForm.teacherId = teacherRow.id;
    state.ruleForm.teacherName = teacherRow.nickName;
	}
	state.dialog.isShowDialog = true;
	getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
// const onSubmit = () => {
// 	closeDialog();
// 	emit('refresh');
// 	// if (state.dialog.type === 'add') { }
// };
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await saveOrUpdateTeach(state.ruleForm)
      if (res.status === 200) {
        ElMessage.success('更改成功');
        emit('refresh');
      }else{
        ElMessage.error('更改失败');
      }
      closeDialog();

    }
  })
}

// 初始化机构数据
const getMenuData = async () => {
  const res = await orgSchoolTree()
  if (res.status === 200) {
    state.sysOrgData = res.data;
  }
};

const clearForm = () => {
  state.ruleForm = {
    id:'',
    isDelete:0,
    isHeadTeacher:0,
    orgId:'',
    orgName:'',
    teachSubject:'',
    teacherId:'',
    teacherName:'',
  }
}
// 暴露变量
defineExpose({
	openDialog,
});
</script>
